﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;

namespace Obsidian.Edm
{
    public class EntityListField<T> : ListField, IModelField where T : IModel, new()
    {
        public override FieldType Type
        {
            get { return FieldType.EntityList; }
        }

        private IList<T> _value = new List<T>();
        /// <summary>
        /// 获取值
        /// </summary>
        public IList<T> Value
        {
            get
            {
                if (_isNull)
                    throw new Exception("空值状态无法获得该属性值");
                return this._value;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get
            {
                if (_isNull) return null;
                IList<IModel> list = new List<IModel>(this._value.Count);
                foreach (IModel m in this._value)
                    list.Add(m);
                return list;
            }
        }


        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(IList<T> value)
        {
            if (!base._assigned)
                base._assigned = true;
            this._isNull = false;
            this._value = value;
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(object value)
        {
            if (value == null)
            {
                return this.SetNull();
            }
            else
            {
                IList<T> val = (IList<T>)value;
                return this.Set(val);
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            try
            {
                this.Set(value);
                return true;
            }
            catch
            {
                this.SetNull();
                return false;
            }

        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this._value.Clear();
            this._isNull = true;
            return base.Model;
        }

        public new string ToString()
        {
            return this.IsNull ? null : ModelUtil.ToJsonString(this._value);
        }

        public EntityListField(IModel model)
        {
            this.InitField(model);
        }

        public EntityListField(IModel model, string alias)
        {
            this.InitField(model, null, alias);
        }

        public EntityListField(IModel model, string alias, IList<T> defaultValue)
        {
            this.InitField(model, null, alias);
            if (defaultValue != null)
            {
                this._value = defaultValue;
                this._isNull = false;
            }
        }

        public void Add(T val)
        {
            this._value.Add(val);
            this._assigned = true;
            if (this._isNull == true)
                this._isNull = false;
        }

        public int Count
        {
            get { return this._value.Count; }
        }

        public void Clear()
        {
            this._value.Clear();
            this._assigned = true;
            this._isNull = true;
        }

    }
}
